<template>
	<view class="home">
		<!-- 自定义导航栏 -->
		<navbar></navbar>
		<tab :list="tabList" @tab="tab" :tabIndex="tabIndex"></tab>
		<view class="home-list">
			<list ref="tabList" :tab="tabList" :activeIndex="activeIndex" @change="change"></list>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				title: 'Hello',
				tabList: [],
				tabIndex: 0,
				activeIndex: 0
			}
		},
		computed: {
			...mapState(['userinfo'])
		},
		watch: {
			userinfo(newVal) {
				this.getLabel();
			}
		},
		onLoad() {
			uni.$on('labelChange', () => {
				this.tabList = [];
				this.tabIndex = 0;
				this.activeIndex = 0;
				this.getLabel();
			})
		},
		methods: {
			change(current) {
				this.tabIndex = current;
				this.activeIndex = current;
			},
			getLabel() {
				// 调用云函数方法
				this.$api.get_label().then((res) => {
					console.log(res);
					const {
						data
					} = res;
					data.unshift({
						name: '全部'
					})
					this.tabList = data;
				})
			},
			tab({
				data,
				index
			}) {
				console.log(data, index);
				this.activeIndex = index;
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: hidden;

		.home-list {
			flex: 1;
			box-sizing: border-box;
		}
	}
</style>
